package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.DyeMapper;
import com.zhijian.medical.entity.po.BeforeDye;
import com.zhijian.medical.entity.po.Dye;
import com.zhijian.medical.entity.vo.request.BeforeDyeSearchVo;
import com.zhijian.medical.entity.vo.request.DyeSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.DyeSearchVo;
import com.zhijian.medical.entity.vo.response.BeforeDyeListVo;
import com.zhijian.medical.entity.vo.response.BeforeDyeVo;
import com.zhijian.medical.entity.vo.response.DyeVo;
import com.zhijian.medical.enums.DyeEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.BeforeDyeService;
import com.zhijian.medical.service.DyeService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 脱水记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-11-17 15:26:31
 */
@Slf4j
@Service
public class DyeServiceImpl implements DyeService {

    @Autowired
    private DyeMapper mapper;

    @Autowired
    private BeforeDyeService beforeDyeService;

    @Override
    public Dye selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DyeVo selectVoById(Long id) {
        DyeVo dyeVo = mapper.selectVoById(id);
        if (Objects.isNull(dyeVo)) {
            return null;
        }
        BeforeDyeVo beforeDehydrationVo = beforeDyeService.selectVoById(dyeVo.getBeforeId());
        dyeVo.setSampleList(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getSampleList() : null);
        dyeVo.setProcessNumber(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getProcessNumber() : null);
        return dyeVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Dye dye) {
        Date now = new Date();
        dye.setCreateTime(now);
        dye.setUpdateTime(now);
        return mapper.insert(dye) > 0 ? dye.getId() : 0L;
    }

    @Override
    public boolean update(Dye dye) {
        Date now = new Date();
        dye.setUpdateTime(now);
        return mapper.updateByPrimaryKey(dye) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Dye record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(DyeSearchVo dyeSearchVo) {
        return mapper.count(dyeSearchVo);
    }

    @Override
    public List<DyeVo> selectList(DyeSearchVo dyeSearchVo) {
        List<DyeVo> dyeVos = mapper.selectList(dyeSearchVo);
        if (CollectionUtil.isEmpty(dyeVos)) {
            return Collections.emptyList();
        }
        dyeVos.forEach(dehydrationVo -> {
            BeforeDyeVo beforeDehydrationVo = beforeDyeService.selectVoById(dehydrationVo.getBeforeId());
            dehydrationVo.setSampleList(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getSampleList() : null);
            dehydrationVo.setProcessNumber(Objects.nonNull(beforeDehydrationVo) ? beforeDehydrationVo.getProcessNumber() : null);
        });
        return dyeVos;
    }

    @Override
    public Page<DyeVo> selectPage(DyeSearchVo dyeSearchVo) {
        //先直接查总数
        int count = count(dyeSearchVo);
        //分页
        Page<DyeVo> page = new Page<>(true, count, dyeSearchVo.getPageNum(), dyeSearchVo.getPageSize());
        dyeSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DyeVo> responseVos = this.selectList(dyeSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Dye> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean save(DyeSaveOrUpdateVo dyeSaveOrUpdateVo) {
        //搜一下这个容器有没有已经准备好的数据
        List<BeforeDyeListVo> beforeDehydrationListVos = beforeDyeService.selectList(BeforeDyeSearchVo.builder()
                .containerNumber(dyeSaveOrUpdateVo.getContainerNumber())
                .status(DyeEnum.WAIT_DYE.getCode().byteValue())
                .build());
        AssertUtil.check(CollectionUtil.isEmpty(beforeDehydrationListVos), "此容器没有录入待染色样本");
        Dye dehydration = new Dye();
        dehydration.setContainerNumber(dyeSaveOrUpdateVo.getContainerNumber());
        dehydration.setDeviceNumber(dyeSaveOrUpdateVo.getDeviceNumber());
        dehydration.setBeforeId(beforeDehydrationListVos.get(0).getId());
        dehydration.setOperateUserId(CurrentUserHolder.getUser().getId());
        this.insert(dehydration);
        //把准备表改成脱水中
        beforeDyeService.updateByPrimaryKeySelective(BeforeDye.builder()
                .id(beforeDehydrationListVos.get(0).getId())
                .status(DyeEnum.IN_DYE.getCode().byteValue())
                .build());
        return true;
    }

}